
package logical_spaces.beans;

import Entity.TableSpace;
import Entity.TempFile;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import logical_spaces.business.LogicalSpaceBusiness;
import logical_spaces.business.TableSpaceBusiness;
import logical_spaces.business.TableSpaceDataModel;
import physical_spaces.business.PhysicalSpaceBusiness;
import login.beans.ConectionBean;
import physical_spaces.business.TempFileDataModel;

/**
 *
 * @author JuanFelipe
 */
@ManagedBean
@SessionScoped

public class ModifyTableSpaceBeanTemporary implements Serializable {

    private List<TableSpace> tableSpaces;
    private TableSpaceDataModel mediumTableSpaceModel;
    private static final long serialVersionUID = 308L;
    
    //datafiles per tablespace
    private List<TempFile> tempFields ;
    private TempFileDataModel mediumTempFileModel;
    private TempFile selectedTempFile;
    
    private TableSpace selectedtablespace;
    private String nameTableSpace;
    private String newNameTableSpace;
    private String nameTempFile;
    private String sizeTempFile;
    private String tempFileSizeOption;
    private int index;
    //propiedades componentes
    private String disabled;
    private String disabledParent;
    private String modeOptionsDisabled;
    private String message;
    //editar
    private String online;
    
    private Connection conn;

    public ModifyTableSpaceBeanTemporary(  ) {
        
        FacesContext context = javax.faces.context.FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
        ConectionBean conectionBean =(ConectionBean) session.getAttribute("conectionBean");        
        this.conn = conectionBean.currentConnection;
        
        online = "true";
        message = "ocurrio un error";
        modeOptionsDisabled = "false";
        tempFileSizeOption = "K";
        
        tableSpaces = new ArrayList<>();
        chargeDataTableSpaces();
        mediumTableSpaceModel = new TableSpaceDataModel(tableSpaces);
       
        index = 0;
        disabled = "true";
        tempFields = new ArrayList<>();
                
    }

    public String getModeOptionsDisabled() {
        return modeOptionsDisabled;
    }

    public void setModeOptionsDisabled(String modeOptionsDisabled) {
        this.modeOptionsDisabled = modeOptionsDisabled;
    }

    public String getNewNameTableSpace() {
        return newNameTableSpace;
    }

    public void setNewNameTableSpace(String newNameTableSpace) {
        this.newNameTableSpace = newNameTableSpace;
    }

    public List<TableSpace> getTableSpaces() {
        return tableSpaces;
    }

    public void setTableSpaces(List<TableSpace> tableSpaces) {
        this.tableSpaces = tableSpaces;
    }

    public TableSpaceDataModel getMediumTableSpaceModel() {
        return mediumTableSpaceModel;
    }

    public void setMediumTableSpaceModel(TableSpaceDataModel mediumTableSpaceModel) {
        this.mediumTableSpaceModel = mediumTableSpaceModel;
    }

    public TableSpace getSelectedtablespace() {
        return selectedtablespace;
    }

    public void setSelectedtablespace(TableSpace selectedtablespace) {
        this.selectedtablespace = selectedtablespace;
    }

    public String getNameTableSpace() {
        return nameTableSpace;
    }

    public void setNameTableSpace(String nameTableSpace) {
        this.nameTableSpace = nameTableSpace;
    }

    public String getNameTempFile() {
        return nameTempFile;
    }

    public void setNameTempFile(String nameTempFile) {
        this.nameTempFile = nameTempFile;
    }

    public String getSizeTempFile() {
        return sizeTempFile;
    }

    public void setSizeTempFile(String sizeTempFile) {
        this.sizeTempFile = sizeTempFile;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getDisabled() {
        return disabled;
    }

    public void setDisabled(String disabled) {
        this.disabled = disabled;
    }

    public String getDisabledParent() {
        return disabledParent;
    }

    public void setDisabledParent(String disabledParent) {
        this.disabledParent = disabledParent;
    }

    public String getOnline() {
        return online;
    }

    public void setOnline(String online) {
        this.online = online;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<TempFile> getTempFields() {
        return tempFields;
    }

    public void setTempFields(List<TempFile> tempFields) {
        this.tempFields = tempFields;
    }

    public TempFileDataModel getMediumTempFileModel() {
        return mediumTempFileModel;
    }

    public void setMediumTempFileModel(TempFileDataModel mediumTempFileModel) {
        this.mediumTempFileModel = mediumTempFileModel;
    }

    public TempFile getSelectedTempFile() {
        return selectedTempFile;
    }

    public void setSelectedTempFile(TempFile selectedTempFile) {
        this.selectedTempFile = selectedTempFile;
    }

    public Connection getConn() {
        return conn;
    }

    public void setConn(Connection conn) {
        this.conn = conn;
    }

    public String getTempFileSizeOption() {
        return tempFileSizeOption;
    }

    public void setTempFileSizeOption(String tempFileSizeOption) {
        this.tempFileSizeOption = tempFileSizeOption;
    }
    
    public void cancel(){
        index = 0;
        disabledParent = "false";
        disabled = "true";
    }
    
    public void updateModoDisabledState(){
        
        if("true".equals(online))
            modeOptionsDisabled = "false";
        if("false".equals(online))
            modeOptionsDisabled = "true";            
        
    }
    
    public void changeTab(){
        FacesContext context = FacesContext.getCurrentInstance();  
        if(selectedtablespace == null)
            context.addMessage(null,new FacesMessage("Error ","Por favor seleccione un Table Space"));
        else{
            nameTableSpace = selectedtablespace.getName();
            newNameTableSpace = nameTableSpace;
            disabledParent = "true";
            disabled = "false";
            index++;
            chargeTempDataFiles();
            mediumTempFileModel = new TempFileDataModel(tempFields);
        }    
    }

    private void chargeDataTableSpaces() {
        LogicalSpaceBusiness lsb;
        try {
            lsb = new LogicalSpaceBusiness( conn );
            tableSpaces = lsb.chargeTemporaryTableSpaceData();
        } catch (SQLException ex) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_WARN,
                            "Error de Consulta!",
                            "Please Try Again! " + ex.getMessage()));
        }
    }
    private void chargeTempDataFiles() {
        PhysicalSpaceBusiness psb = new PhysicalSpaceBusiness( conn );
        tempFields = psb.selectTempFilesPerTableSpace(nameTableSpace);
    }
    
    public void modifyTablespace() {

        FacesContext context = FacesContext.getCurrentInstance();
        TableSpaceBusiness tableSpaceBusinees = new TableSpaceBusiness( conn );
        if (online.equals("true")) 
            context.addMessage(null, new FacesMessage("Mensaje ", tableSpaceBusinees.crearQueryEstado(nameTableSpace, "TEMPFILE ONLINE")));
        else
            context.addMessage(null, new FacesMessage("Mensaje ", tableSpaceBusinees.crearQueryEstado(nameTableSpace, "TEMPFILE OFFLINE")));
       
        chargeTempDataFiles();
        mediumTempFileModel = new TempFileDataModel(tempFields);

    }
    
    public void addTempFile() {
        
        FacesContext context = FacesContext.getCurrentInstance();
        TableSpaceBusiness tableSpaceBusinees=new TableSpaceBusiness( conn );
        context.addMessage(null,new FacesMessage("Mensaje ",tableSpaceBusinees.crearQueryAgregarTempFile(nameTableSpace,nameTempFile,sizeTempFile,tempFileSizeOption)));
        chargeTempDataFiles();
        mediumTempFileModel = new TempFileDataModel(tempFields);
        
    }
    
    public void deleteTempFile() {
        
        FacesContext context = FacesContext.getCurrentInstance();
        TableSpaceBusiness tableSpaceBusinees = new TableSpaceBusiness( conn );
        if (selectedTempFile == null) {
            context.addMessage(null, new FacesMessage("Error ", "Por favor seleccione un DataFile"));
        } else {
            context.addMessage(null,new FacesMessage("Mensaje ",tableSpaceBusinees.crearQueryEliminarTempFilePerTableSpace(nameTableSpace,selectedTempFile.getName())));
            chargeTempDataFiles();
            mediumTempFileModel = new TempFileDataModel(tempFields);
        }
    }
    


}
